package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.SysPermissionMapper;
import cn.wzut.edut.dao.mapper.SysRolePermissionRelMapper;
import cn.wzut.edut.dao.pojo.table.*;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.LinkedList;
import java.util.List;

/**
 * 权限管理
 */
@Repository
public class Permission {
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysRolePermissionRelMapper sysRolePermissionRelMapper;
    /**
     * 根据角色id删除角色权限表
     */
    public boolean delRolePermissionInRoleId(String role_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id",role_id);
        sysRolePermissionRelMapper.delete(queryWrapper);
        return true;
    }


    /**
     * 根据角色id获取角色的权限
     */
    public List<SysPermission> selectPermissionInRoleId(String role_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id",role_id);
        List<SysRolePermissionRel> list1 = sysRolePermissionRelMapper.selectList(queryWrapper);
        List<SysPermission> list = new LinkedList<>();
        for (int i = 0;i<list1.size();i++){
            QueryWrapper<SysPermission> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",list1.get(i).getPermissionId());
            list.add(sysPermissionMapper.selectOne(queryWrapper1));
        }
        return list;
    }
    /**
     * 根据角色id获取角色的权限,分页,传入需要的页码current和每页显示的数量size
     */
    public List<SysPermission> selectPermissionInRoleIdPage(String role_id,long current,long size){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id",role_id);
        List<SysRolePermissionRel> list1 = sysRolePermissionRelMapper.selectList(queryWrapper);
        List<SysPermission> list = new LinkedList<>();
        for (int i = 0;i<list1.size();i++){
            QueryWrapper<SysPermission> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",list1.get(i).getPermissionId());
            list.add(sysPermissionMapper.selectOne(queryWrapper1));
        }
        List<SysPermission> list2 = new LinkedList<>();
        for(long i = current*size-size;i<current*size;i++){list2.add(list.get((int) i));}
        return list2;
    }
    /**65
     * 按照角色id和菜单id删除角色菜单关系表
     */
    public boolean delSysRolePermission(String role_id,String permission_id){
        /**
         * queryWrapper：按照角色id和用户id删除角色用户关系表
         */
//        try {
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", role_id).in("permission_id",permission_id);
        sysRolePermissionRelMapper.delete(queryWrapper);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    /**
     * 分页
     * 获取所有权限
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysPermission> selectAllPermissionPage(long current,long size){
        return sysPermissionMapper.selectPage(new Page<SysPermission>(current,size),null).getRecords();
    }
    /**
     * 分页
     * 查询权限
     * 输入权限名
     * @return 权限
     */
    public List<SysPermission> selectSysPermissionInNamePage(long current,long size,String name){
        /**
         * q1：以菜单名查找菜单
         */
        QueryWrapper<SysPermission> q1 = new QueryWrapper<>();
        q1.in("Permission_name",name);
        return sysPermissionMapper.selectPage(new Page<SysPermission>(current,size),q1).getRecords();
    }
    /**
     * 根据角色id获取角色的权限
     */
    public long countPermissionInRoleId(String role_id){
        QueryWrapper<SysRolePermissionRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id",role_id);
        List<SysRolePermissionRel> list1 = sysRolePermissionRelMapper.selectList(queryWrapper);
        List<String> list = new LinkedList<>();
        for (int i = 0;i<list1.size();i++){
            list.add(list1.get(i).getPermissionId());
        }
        QueryWrapper<SysPermission> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id",list1);
        return sysPermissionMapper.selectCount(queryWrapper1);
    }
    /**
     * 总数
     * 获取所有权限
     * 传入需要的页码current和每页显示的数量size
     */
    public long countAllPermissionPage(){
        return sysPermissionMapper.selectCount(null);
    }
    /**
     * 总数
     * 查询权限
     * 输入权限名
     *
     */
    public long countSysPermissionInNamePage(String name){
        /**
         * q1：以菜单名查找菜单
         */
        QueryWrapper<SysPermission> q1 = new QueryWrapper<>();
        q1.in("Permission_name",name);
        return sysPermissionMapper.selectCount(q1);
    }

    /**
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     * 已整理
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础查询
     * ===================================================================================================
     */

    /**
     * 查询权限信息
     */
    public List<SysPermission> selectSysPermission(String id,String res_url,String permission_name){
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(res_url),"res_url",res_url);
        queryWrapper.in(!StringUtils.isEmpty(permission_name),"permission_name",permission_name);
        return sysPermissionMapper.selectList(queryWrapper);
    }
    /**
     * 查询权限信息数量
     */
    public long countSysPermission(String id,String res_url,String permission_name){
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(res_url),"res_url",res_url);
        queryWrapper.in(!StringUtils.isEmpty(permission_name),"permission_name",permission_name);
        return sysPermissionMapper.selectCount(queryWrapper);
    }
    /**
     * 查询权限信息
     * 分页
     */
    public List<SysPermission> selectSysPermissionPage(String id,String res_url,String permission_name,long current,long size){
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(res_url),"res_url",res_url);
        queryWrapper.in(!StringUtils.isEmpty(permission_name),"permission_name",permission_name);
        return sysPermissionMapper.selectPage(new Page<SysPermission>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询权限信息
     * 列表查询
     */
    public List<SysPermission> selectSysPermissionList(List<String> id,List<String> res_url,List<String> permission_name){
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(res_url!=null,"res_url",res_url);
        queryWrapper.in(permission_name!=null,"permission_name",permission_name);
        return sysPermissionMapper.selectList(queryWrapper);
    }
    /**
     * 查询权限信息数量
     * 列表查询
     */
    public long countSysPermissionList(List<String> id,List<String> res_url,List<String> permission_name){
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(res_url!=null,"res_url",res_url);
        queryWrapper.in(permission_name!=null,"permission_name",permission_name);
        return sysPermissionMapper.selectCount(queryWrapper);
    }
    /**
     * 查询权限信息
     * 列表查询
     * 分页
     */
    public List<SysPermission> selectSysPermissionPage(List<String> id,List<String> res_url,List<String> permission_name,long current,long size){
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(res_url!=null,"res_url",res_url);
        queryWrapper.in(permission_name!=null,"permission_name",permission_name);
        return sysPermissionMapper.selectPage(new Page<SysPermission>(current,size),queryWrapper).getRecords();
    }


    /**
     * ===================================================================================================
     * 基础删除
     * ===================================================================================================
     */



    /**
     * ===================================================================================================
     * 基础更新
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础新增
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 级联删除
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 复杂查询
     * ===================================================================================================
     */

}
